package ik.xcj.hostpital.manage.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ik.xcj.hostpital.manage.ex.ServiceException;
import ik.xcj.hostpital.manage.mapper.IDepartmentMapper;
import ik.xcj.hostpital.manage.pojo.dto.DepartmentAddNewDTO;
import ik.xcj.hostpital.manage.pojo.dto.DepartmentUpdateDTO;
import ik.xcj.hostpital.manage.pojo.entity.Department;
import ik.xcj.hostpital.manage.pojo.vo.DepartmentListItemVO;
import ik.xcj.hostpital.manage.pojo.vo.DepartmentStandardVO;
import ik.xcj.hostpital.manage.service.IDepartmentService;
import ik.xcj.hostpital.manage.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 处理科室数据的业务
 *
 * @author ik.xcj
 * @version 0.0.1
 */
@Slf4j
@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private IDepartmentMapper departmentMapper;

    @Override
    public void addNew(DepartmentAddNewDTO departmentAddNewDTO) {
        log.debug("开始处理【添加科室数据】的业务，参数：{}",departmentAddNewDTO);
        String depname = departmentAddNewDTO.getDepname();
        int countByDepname = departmentMapper.countByDepname(depname);
        if (countByDepname>0){
            String message = "添加科室失败，科室名称已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        Department department = new Department();
        BeanUtils.copyProperties(departmentAddNewDTO,department);
        log.debug("即将插入科室的数据，参数：{}",department);
        int rows = departmentMapper.insert(department);
        if (rows !=1){
            String message = "添加科室数据失败，服务器忙，请稍后尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【删除科室数据】的业务，参数：{}",id);
        DepartmentStandardVO standardVO = departmentMapper.getStandardById(id);
        if (standardVO == null){
            String message = "删除科室数据失败，尝试访问的数据不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("即将删除科室数据，参数：{}",id);
        int rows = departmentMapper.deleteById(id);
        if (rows != 1){
            String message = "删除科室数据失败，服务器忙，请稍后尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void updateInfoById(Long id, DepartmentUpdateDTO departmentUpdateDTO) {
        log.debug("开始处理【修改科室数据】的业务，id = {}，新的数据={}",id,departmentUpdateDTO);
        DepartmentStandardVO departmentStandardVO = departmentMapper.getStandardById(id);
        if (departmentStandardVO == null){
            String message = "修改科室数据失败，尝试访问的数据不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Department department = new Department();
        BeanUtils.copyProperties(departmentUpdateDTO,department);
        department.setId(id);
        log.debug("即将修改科室数据，参数：{}",department);
        int rows = departmentMapper.update(department);
        if (rows != 1){
            String message = "修改科室数据失败，服务器忙，请稍后尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public DepartmentStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查找科室数据】，参数：{}",id);
        DepartmentStandardVO depStandardVO = departmentMapper.getStandardById(id);
        if (depStandardVO == null){
            String message ="查找科室数据失败，尝试访问的数据不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("即将返回查询结果",depStandardVO);
        return depStandardVO;
    }

    @Override
    public PageInfo<DepartmentListItemVO> list(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        log.debug("开始处理【查询科室列表】的业务，参数：无");
        List<DepartmentListItemVO> departmentList = departmentMapper.list();
        log.debug("即将返回查询结果：{}",departmentList);
        return new PageInfo<>(departmentList);
    }

}
